package com.yanqu.road.server.gameplayer.module.activity.commonteam;

import com.yanqu.road.entity.activity.commonteam.CommonTeamConfigData;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.Protocol;

public class CommonTeamModule extends GeneralModule {

    public CommonTeamModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        //同步配置
        syncConfig();
        //通知跨服，登录后处理
        this.crossAfterLoginHandle();
    }

    /**
     * 同步配置和活动数据
     */
    public void syncConfig() {
        for(CommonTeamConfigData configData : CommonTeamMgr.getConfigDataMap().values()) {
            //判断系统是否解锁
            if (!CommonTeamMgr.checkSystemUnlock(player, configData.getActivityInfo().getType())) {
                continue;
            }
            CommonTeamProto.CommonTeamConfigSync.Builder msg = CommonTeamProto.CommonTeamConfigSync.newBuilder();
            msg.setActivityId(configData.getActivityId());
            CommonTeamProto.CommonTeamConfig.Builder builder = CommonTeamProto.CommonTeamConfig.newBuilder();
            builder.setCreateTeamTime(configData.getCreateTeamTime());
            builder.setTeamNameLengthLimit(configData.getNameLengthMax());
            builder.setApplyMaxLimit(configData.getApplyMax());
            builder.setUserApplyMaxLimit(configData.getUserApplyMax());
            builder.setJoinKuFu(configData.getJoinCrossTeamFlag());
            builder.setCreateNeedIngots(configData.getCreateNeedIngots());
            builder.setJoinCd(configData.getJoinCd());
            msg.setConfig(builder);
            //同步
            player.sendPacket(Protocol.U_COMMON_TEAM_CONFIG_SYNC, msg);
        }
    }

    /**
     * 通知跨服登录后处理
     */
    private void crossAfterLoginHandle() {
        for(CommonTeamConfigData configData : CommonTeamMgr.getConfigDataMap().values()) {
            //判断系统是否解锁
            if (!CommonTeamMgr.checkSystemUnlock(player, configData.getActivityInfo().getType())) {
                continue;
            }
            CommonTeamProto.CommonTeamCrossAfterLoginHandleReqMsg.Builder builder = CommonTeamProto.CommonTeamCrossAfterLoginHandleReqMsg.newBuilder();
            builder.setActivityId(configData.getActivityInfo().getActivityId());
            player.sendUnionActivityGroupPacket(Protocol.C2_COMMON_TEAM_AFTER_LOGIN_HANDLE, builder, builder.getActivityId());
        }
    }
}
